package task05_01;

import java.util.ArrayList;
import java.util.Iterator;


public class Task05_01 {
    public static void main(String[] args) {
        //ArrayList存取String对象
        ArrayList list=new ArrayList();
        //add()存  get()取
        list.add("hello");
        list.add("Java");
        System.out.println("集合长度："+list.size());
        System.out.println("集合的第一个元素是："+list.get(0)+"\n");

        //ArrayList存取Student对象
        ArrayList list2=new ArrayList();
        Student s1=new Student("ZhangSan",19);
        Student s2=new Student("LiSi",20);
        list2.add(s1);
        list2.add(s2);
        System.out.println("集合长度："+list2.size());
        System.out.println("集合的第一个元素是："+list2.get(0)+"\n");

        System.out.println("集合中所有元素有：");
        //集合遍历
        // for循环语句遍历
        System.out.println("for循环语句遍历:");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
        for(int i=0;i<list2.size();i++){
            System.out.println(list2.get(i));
        }

        System.out.println();

        //Iterator接口遍历
        System.out.println("Iterator接口遍历:");
        //1.获取迭代器对象
        Iterator iterator=list.iterator();
        //2.迭代器接口中有2个方法：
        //(1)hasNext()是否存在下一个元素，
        //(2)Next()取出下一个元素
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        Iterator iterator2=list2.iterator();
        while(iterator2.hasNext()){
            System.out.println(iterator2.next());
        }

        System.out.println();

        //foreach语句
        System.out.println("foreach语句遍历:");
        for(Object obj:list){
            System.out.println(obj);
        }
        for(Object obj2:list2){
            System.out.println(obj2);
        }
    }
}
